package com.tsmti.webservice.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.serializer.ValueFilter;
import com.tsmti.webservice.entity.ReturnMsg;
import com.tsmti.webservice.entity.ServiceInfo;
import com.tsmti.webservice.service.PimsService;
import com.tsmti.webservice.util.ServiceInfoUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;
import org.springframework.web.context.support.SpringBeanAutowiringSupport;

import javax.jws.WebService;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;


/**
 * @Package：service.impl
 * @Class：PimsServiceImpl
 * @Description： TODO
 * @Author：zcw
 * @Date：Created in 2018/7/9 17:03
 * @Company:
 * @Version：
 * @Modified By:
 */
@Service
@WebService(endpointInterface = "com.tsmti.webservice.service.PimsService",
        serviceName = "PimsService",
        targetNamespace = "http://service.webservice.tsmti.com/")
public class PimsServiceImpl extends SpringBeanAutowiringSupport implements PimsService {
    private static Logger log = Logger.getLogger(PimsServiceImpl.class);
    private final static String RESULTTYPE_XML = "xml";
    @Override
    public String runService(String trackId,String orgId, String content,String resultType) {
        ReturnMsg msg = new ReturnMsg();
        try {
            ServiceInfo serviceInfo = ServiceInfoUtils.getService(trackId,orgId);
            if(serviceInfo != null){
                String funName = serviceInfo.getName();
                String className = serviceInfo.getClassName();
                Class<?> clazz = Class.forName(className);
                boolean hasMethod = false;
                if(!StringUtils.isEmpty(funName)){
                    //判断调用方法是否存在
                    Method[] methods = clazz.getMethods();
                    for (Method method:methods) {
                        if (funName.equals(method.getName())) {
                            hasMethod = true;
                            break;
                        }
                    }
                    if(hasMethod){
                        //存在执行方法
                        log.debug("准备执行方法"+funName);
                        Method method = clazz.getMethod(funName, new Class[]{String.class,String.class});
                        log.debug("执行方法结束"+funName);
                        msg = (ReturnMsg) method.invoke(clazz.newInstance(), new Object[]{orgId,content});
                    }else {
                        msg.setState(0);
                        msg.setMessage("交易代码为"+trackId+"调用方法不存在，请检查tranId");
                    }
                }
            }else {
                msg.setState(0);
                msg.setMessage("交易代码为"+trackId+"方法没有配置，请检查配置信息");
            }
        }catch (InvocationTargetException e ){
            msg.setState(0);
            msg.setMessage("交易代码为"+trackId+"的调用方法发生错误："+e.getTargetException() == null ? e.getMessage() : e.getTargetException().getMessage());
            log.error("交易代码为"+trackId+"的调用方法发生错误：",e);
        } catch (Exception e)
        {
            msg.setState(0);
            msg.setMessage("交易代码为"+trackId+"的调用方法发生错误："+e.toString());
            log.error("交易代码为"+trackId+"的调用方法发生错误：",e);
        }
        String result = "";
        if(RESULTTYPE_XML.equals(resultType)){
            //返回XML
            result ="";
        }else {
            //返回JSON
            result =  JSON.toJSONString(msg, filter, SerializerFeature.SortField);
        }
        return result;
    }

    /**
     * 用于解决JSON 字段值NULL不显示
     */
    private ValueFilter filter = new ValueFilter() {
        @Override
        public Object process(Object obj, String s, Object v) {
            if (v == null){
                return "";
            }
            return v;
        }
    };
}
